package com.GlobalPaint.app.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.gethaoyouBean;
import com.GlobalPaint.app.ui.Home.friend.popupActivity;
import com.GlobalPaint.app.ui.Mine.Personal.PersonalZhanshi;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class tianjiaFriendsAdapter extends XRecyclerView.Adapter<tianjiaFriendsAdapter.ViewHoler>{
    private gethaoyouBean.DataBean bean;
    private popupActivity popupActivity;
    String nick1;
    public tianjiaFriendsAdapter(gethaoyouBean.DataBean data, popupActivity popupActivity) {
        this.popupActivity=popupActivity;
        bean=data;
    }

    @Override
    public tianjiaFriendsAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.frament_tianjia_friends_item ,parent, false);
        tianjiaFriendsAdapter.ViewHoler holder = new tianjiaFriendsAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(tianjiaFriendsAdapter.ViewHoler holder, final int position) {
        final String Phone=bean.getFUsername()==
                null?"":bean.getFUsername();

        if (Phone.length() > 4) {
            String phone1 = (Phone.substring(0, 3));
            String phone2 = Phone.substring(Phone.length() - 3, Phone.length());
            holder.tv_name.setText(phone1 + "***" + phone2);
        }
        String nick=bean.getFNickName()==
                null?"":bean.getFNickName();

        holder.tv_nick.setText(nick);
        String Url= bean.getAvatar()== null?"wu":
                DataManager.userEntity.getAvatar();
        if(Url.equals("wu")){
            Picasso.with(popupActivity).load(R.drawable.touxiang).into(holder.imge);
        }else {
            Picasso.with(popupActivity).load(Constants.JASON_SERVICE_URL_TOUXIANG+Url)
                    .into(holder.imge);
        }

        holder.tv_guiji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = DataManager.userEntity.getUserName();
                String jid=bean.getFUsername();
                if(bean.getFNickName()!=null){
                     nick1=bean.getFNickName();
                }else {
                     nick1=bean.getFNickName();
                }
                if(DataManager.userEntity.getUserName().equals(Phone)){
                    Toast.makeText(view.getContext(),"请重新输入账号",Toast.LENGTH_LONG).show();
                    return;
                }
                popupActivity.tianjiaHaoyou(userName,jid,nick1);
            }
        });

        holder.imge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), PersonalZhanshi.class);
                    intent.putExtra("userid",bean.getFUserId());
                    view.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public class ViewHoler extends RecyclerView.ViewHolder {

        private final TextView tv_name;
        private final TextView tv_nick;
        private final TextView tv_guiji;
        private final ImageView imge;
        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_nick = (TextView)itemView.findViewById(R.id.tv_nick);
            tv_guiji = (TextView)itemView.findViewById(R.id.tv_guiji);
            imge = (ImageView)itemView. findViewById(R.id.imge);
        }
    }
}
